package utils.document;

import com.artofsolving.jodconverter.DocumentConverter;
import com.artofsolving.jodconverter.openoffice.connection.OpenOfficeConnection;
import com.artofsolving.jodconverter.openoffice.connection.SocketOpenOfficeConnection;
import com.artofsolving.jodconverter.openoffice.converter.OpenOfficeDocumentConverter;

import java.io.*;
import java.net.ConnectException;
/**
 * 利用jodconverter(基于OpenOffice服务)将文件(*.doc、*.docx、*.xls、*.ppt)转化为html格式或者pdf格式，
 * 使用前请检查OpenOffice服务是否已经开启, OpenOffice进程名称：soffice.exe | soffice.bin
 */
public class Doc2HtmlUtil {
    private static Doc2HtmlUtil doc2HtmlUtil;
    /*** 获取Doc2HtmlUtil实例 */
    public static synchronized Doc2HtmlUtil getDoc2HtmlUtilInstance() {
        if (doc2HtmlUtil == null) {
            doc2HtmlUtil = new Doc2HtmlUtil();
        }
        return doc2HtmlUtil;
    }

    /*** 转换文件成pdf */
    public String file2pdf(InputStream fromFileInputStream, String toFilePath,String type,String filename) throws IOException {
        String docFileName;
        String htmFileName;
        System.out.println("文件开始转换...");
        if("doc".equals(type)){
            docFileName = filename + ".doc";
            htmFileName = filename + ".pdf";
        }else if("docx".equals(type)){
            System.out.println("word文档开始转换...");
            docFileName = filename + ".docx";
            htmFileName = filename + ".pdf";
        }else if("xls".equals(type)){
            docFileName = filename + ".xls";
            htmFileName = filename + ".pdf";
        }else if("ppt".equals(type)){
            docFileName = filename + ".ppt";
            htmFileName = filename + ".pdf";
        }else if("pptx".equals(type)){
            docFileName = filename + ".pptx";
            htmFileName = filename + ".pdf";
        }else{
            return null;
        }
        File htmlOutputFile = new File(toFilePath + File.separatorChar + htmFileName);
        File docInputFile = new File(toFilePath + File.separatorChar + docFileName);
        System.out.println("新建文件成功...");
        if (htmlOutputFile.exists())
            htmlOutputFile.delete();
        htmlOutputFile.createNewFile();
        if (docInputFile.exists())
            docInputFile.delete();
        docInputFile.createNewFile();
        /** 由fromFileInputStream构建输入文件  */
        try {
            OutputStream os = new FileOutputStream(docInputFile);
            int bytesRead = 0;
            byte[] buffer = new byte[1024 * 8];
            while ((bytesRead = fromFileInputStream.read(buffer)) != -1) {
                os.write(buffer, 0, bytesRead);
            }
            os.close();
            fromFileInputStream.close();
        } catch (IOException e) {
        }
        // 连接服务
        OpenOfficeConnection connection = new SocketOpenOfficeConnection(8100);
        try {
            connection.connect();
        } catch (ConnectException e) {
            System.err.println("文件转换出错，请检查OpenOffice服务是否启动。");
        }
        System.out.println("服务连接成功");
        // convert 转换
        DocumentConverter converter = new OpenOfficeDocumentConverter(connection);
        converter.convert(docInputFile, htmlOutputFile);
        System.out.println("文档转换成功...");
        //关闭服务链接
        connection.disconnect();
        // 转换完之后删除word文件
        docInputFile.delete();
        return htmFileName;
    }

//    /** 转换功能测试(一) **/
//    public static void main(String[] args) throws IOException {
//        String OpenOfficeHome = "D:/OpenOffice/install/program/";
//        //openoffice开启服务
//        String command = OpenOfficeHome + "soffice -headless -accept=\"socket,host=127.0.0.1,port=8100;urp;\" -nofirststartwizard\n" + "\n";
//        Process pro = Runtime.getRuntime().exec(command);
//        System.out.println("服务开启");
//        //开启连接
//        OpenOfficeConnection connection = new SocketOpenOfficeConnection("127.0.0.1", 8100);
//        connection.connect();
//        System.out.println("连接开启");
//        //开始执行转换
//        Doc2HtmlUtil doc2HtmlUtil = new Doc2HtmlUtil();
//        File file = new File("F:/VideoTest/test.docx");
//        FileInputStream fileInputStream = new FileInputStream(file);
//        doc2HtmlUtil.file2pdf(fileInputStream,"F:/VideoTest/","docx","testPDF");
//        //关闭连接
//        connection.disconnect();
//        System.out.println("连接关闭");
//        // 关闭OpenOffice服务的进程
//        pro.destroy();
//        System.out.println("服务关闭");
//    }
}